package io.wit.modules.sys.controller;

import io.wit.common.utils.R;
import io.wit.modules.sys.entity.SysItemEntity;
import io.wit.modules.sys.service.SysItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 字典管理
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-12-27 14:25:28
 */
@RestController
@RequestMapping("/sys/item")
public class SysItemController {

    @Autowired
	private SysItemService SysItemService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:item:list")
	public List<SysItemEntity> list(){
		List<SysItemEntity> itemList = SysItemService.queryList(new HashMap<String, Object>());

		return itemList;
	}
	/**
	 * 客户端缓存
	 */
	@RequestMapping("/clientsData")
	public R clientsData() {
		//查询列表数据
		Map<String, Object> root = new HashMap<String, Object>();
		List<SysItemEntity> itemList = SysItemService.queryNotItemList();
		Long l = new Long(0);
		for(SysItemEntity im:itemList.stream().filter(a->a.getParentId().equals(l)).collect(Collectors.toList())) {
			Map<String, Object> item = new HashMap<String, Object>();
			for(SysItemEntity ims:itemList.stream().filter(a->a.getParentId().equals(im.getItemId())).collect(Collectors.toList())){
				item.put(ims.getItemId().toString(), ims.getName());
			}
			root.put(im.getItemId().toString(), item);
		}
		return R.ok().put("itemList", root);
	}
	@RequestMapping("/getSelect/{itemId}")
	public R getSelect(@PathVariable("itemId") Long itemId) {
		List<SysItemEntity> itemList = SysItemService.queryItemList(itemId);
		return R.ok().put("itemList", itemList);
	}
	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:item:select")
	public R select(){
		//查询列表数据
		List<SysItemEntity> itemList = SysItemService.queryNotItemList();
		//添加顶级菜单
		SysItemEntity root = new SysItemEntity();
		root.setItemId(0L);
		root.setName("一级字典");
		root.setParentId(-1L);
		root.setOpen(true);
		itemList.add(root);
		return R.ok().put("itemList", itemList);
	}
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{itemId}")
	@RequiresPermissions("sys:item:info")
	public R info(@PathVariable("itemId") Long itemId){
		SysItemEntity item = SysItemService.queryObject(itemId);
		return R.ok().put("item", item);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:item:save")
	public R save(@RequestBody SysItemEntity item){
		SysItemService.save(item);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:item:update")
	public R update(@RequestBody SysItemEntity item){
		SysItemService.update(item);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:item:delete")
	public R delete(final HttpServletRequest request){
        final String itemId = request.getParameter("itemId");
        if(itemId==null || itemId.length() <= 0){
            return R.error("缺少参数");
        }
        SysItemService.deleteBatch(new Long[]{Long.parseLong(itemId)});
		return R.ok();
	}
}